package org.example.computerdesign.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.StrUtil;
import org.example.computerdesign.common.RespBody;
import org.example.computerdesign.common.enums.HttpCode;
import org.example.computerdesign.domain.dto.PatientD;
import org.example.computerdesign.exception.ServiceException;
import org.example.computerdesign.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * @author 朱文权
 * */
@RestController
@SaCheckLogin
public class PatientController {
    @Autowired
    private PatientService patientService;
    /**
     * 增添病患
     * */
    @PostMapping("addPatient")
    public RespBody<?> addPatient(@RequestBody PatientD patient){
        patientService.addPatient(patient.getIdentity(),patient.getName(),patient.getSex());
        return RespBody.success();
    }
    /**
     *删除病患
     */
    @PostMapping("deletePatient")
    public RespBody<?> deletePatient(@RequestBody Long id) {
        if (id == null) {
            return RespBody.fail(new ServiceException(HttpCode.BAD_REQUEST));
        }
        patientService.deletePatient(id);
        return RespBody.success();
    }
    /**
     * 获取一个病人的基本信息
     * */
    @GetMapping("patientCenter")
    public RespBody<?> getPatientInfo(@RequestParam String identity){
        if (StrUtil.isEmpty(identity)) {
            return RespBody.fail(new ServiceException(HttpCode.BAD_REQUEST));
        }
        return RespBody.success(patientService.getPatientInfo(identity));
    }
    /**
     * 分页查询所有病人的基本信息
     * */
    @GetMapping("getAllPatients")
    public RespBody<?> getAll(@RequestParam Integer page,@RequestParam Integer size){
        if(page==null||size==null){
            return RespBody.fail(new ServiceException(HttpCode.BAD_REQUEST));
        }
        return RespBody.success(patientService.getAll(page,size));
    }

}
